package main

import (
	"fmt"
	"gitee.com/kubage88/wf-deploy-plugin-javax/infra"
	"gitee.com/kubage88/wf-deploy-plugin-javax/service"
	"github.com/urfave/cli"
	"os"
)

func main() {
	app := cli.NewApp()
	app.Name = "javax"
	app.Usage = "launch java application"
	app.Version = "0.0.1"

	var jar, options, environments string

	flags := []cli.Flag{
		cli.StringFlag{
			Name:        "file,f",
			Required:    true,
			Usage:       "specify jar file",
			Value:       "",
			Destination: &jar,
		},
		cli.StringFlag{
			Name:        "options,o",
			Usage:       "specify java options",
			Value:       "",
			Destination: &options,
		},
		cli.StringFlag{
			Name:        "environments,e",
			Usage:       "specify environment variables",
			Value:       "",
			Destination: &environments,
		},
	}

	app.Flags = flags

	app.Commands = []cli.Command{
		{
			Name:  "start",
			Usage: "start java service",
			Action: func(*cli.Context) error {
				fmt.Println(fmt.Sprintf("start java service【%s】", jar))
				javax := new(service.Javax)
				if err := javax.Start(jar, options, environments); err != nil {
					infra.Logger.Errorln(err)
					return err
				}

				return nil
			},
		},
		{
			Name:  "stop",
			Usage: "stop java service",
			Action: func(*cli.Context) error {
				fmt.Println(fmt.Sprintf("stop java service【%s】", jar))
				javax := new(service.Javax)
				if err := javax.Stop(jar, options, environments); err != nil {
					infra.Logger.Errorln(err)
					return err
				}

				return nil
			},
		},
		{
			Name:  "restart",
			Usage: "restart java service",
			Action: func(*cli.Context) error {
				fmt.Println(fmt.Sprintf("restart java service【%s】", jar))
				javax := new(service.Javax)
				if err := javax.Restart(jar, options, environments); err != nil {
					infra.Logger.Errorln(err)
					return err
				}

				return nil
			},
		},
	}

	if err := app.Run(os.Args); err != nil {
		fmt.Println(err)
	}
}
